/*
 * Copyright 2011 Fabian Wenzelmann
 * 
 * This file is part of Recognition-Procedures-for-Boolean-Functions.
 * 
 * Recognition-Procedures-for-Boolean-Functions is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Recognition-Procedures-for-Boolean-Functions is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Recognition-Procedures-for-Boolean-Functions.  If not, see <http://www.gnu.org/licenses/>.
 */

package de.wenzelmf.booleanFunction.dnf;

import java.util.HashSet;
import java.util.Iterator;

import de.wenzelmf.booleanFunction.lpb.util.LPBCombinatorialConstants;

// TODO: not very efficient right now...
// TODO: is it correct to cache the hash code?
// TODO: are all methods overwritten that can change the hash code?
/**
 * Implementation of DNF class that is backed by a hash set of clauses.
 * 
 * @author Fabian Wenzelmann
 * @version 1.0
 */
public class HashDNF extends DNF
{

	/**
	 * The HashSet that stores the clauses.
	 */
	private HashSet<Clause> clauseSet;
	
	/**
	 * The hash value, cached for efficency.
	 */
	private int hashCode;
	
	/**
	 * Construct a new, empty DNF.
	 */
	public HashDNF()
	{
		this.clauseSet = new HashSet<Clause>();
		hashCode = 0;
	}
	
	/**
	 * Construct an empty DNF, the hash set is created with the given initial size.
	 * 
	 * @param initialSize The initial size (buckets) of the hash set.
	 */
	public HashDNF(int initialSize)
	{
		this.clauseSet = new HashSet<Clause>((int)(initialSize * LPBCombinatorialConstants.HASH_SIZE_FACTOR));
		hashCode = 0;
	}
	
	/**
	 * Construct an empty DNF, the hash set is created with the given initial size and loadFactor.
	 * 
	 * @param initialSize The initial size (buckets) of the hash set.
	 * @param loadFactor Load factor of the hash set.
	 */
	public HashDNF(int initialSize, float loadFactor)
	{
		this.clauseSet = new HashSet<Clause>((int)(initialSize * LPBCombinatorialConstants.HASH_SIZE_FACTOR),
				loadFactor);
	}
	
	/**
	 * Returns an iterator over the elements in this DNF, removing elements from this DNF by the
	 * {@code remove} method of the iterator is not allowed, it just throws an {@code UnsupportedOperationException}.
	 */
	// TODO: maybe allow remove method, but adjust the hash value
	@Override
	public Iterator<Clause> iterator()
	{
		
		return new Iterator<Clause>()
		{
			Iterator<Clause> setIterator = clauseSet.iterator();
			@Override
			public boolean hasNext()
			{
				return setIterator.hasNext();
			}

			@Override
			public Clause next()
			{
				return setIterator.next();
			}

			@Override
			public void remove()
			{
				throw new UnsupportedOperationException("The remove method for the Iterator of class HashDNF is not supported.");
			}
			
		};
	}

	/**
	 * Returns the size of this DNF (amount of clauses).
	 */
	@Override
	public int size()
	{
		return clauseSet.size();
	}
	
	/**
	 * Returns true if the specified object is contained in this DNF.
	 * 
	 * @param o The element to look for.
	 * @return {@code true} if {@code o} is contained in this DNF.
	 */
	@Override
	public boolean contains(Object o)
	{
		return clauseSet.contains(o);
	}
	
	/**
	 * Add a clause to this DNF.
	 * 
	 * @param c The clause to add.
	 * 
	 * @return {@code true} if the DNF was modified as a result of this action.
	 */
	@Override
	public boolean add(Clause c)
	{
		boolean result = clauseSet.add(c);
		if(result)
		{
			hashCode += c.hashCode();
		}
		return result;
	}
	
	/**
	 * Remove an object from this DNF.
	 * 
	 * @param o The object to remove.
	 * 
	 * @return {@code true} if the collection changed as a result of this call.
	 */
	@Override
	public boolean remove(Object o)
	{
		boolean result = clauseSet.remove(o);
		if(result)
			hashCode -= o.hashCode();
		return result;
	}
	
	/**
	 * Return the hash code of this DNF (that is the sum over all clause hash codes).
	 * 
	 * @return Hash code for this DNF.
	 */
	@Override
	public int hashCode()
	{
		return hashCode;
	}
}
